package com.boc.langxiang.presenter.common;


import com.boc.langxiang.contract.common.BindPhoneContract;
import com.boc.langxiang.entity.request.BindPhoneRequest;
import com.boc.langxiang.entity.request.SmsCodeRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.SmsCodeEntity;
import com.boc.langxiang.entity.response.UserInfo;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class BindPhonePresenter implements BindPhoneContract.Presenter {

    private BindPhoneContract.View mView;
    private final ApiService mService;

    public BindPhonePresenter(BindPhoneContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }


    @Override
    public void bindPhone(String lx, String openid, String phone,String smsCode) {
        mView.showLoading();
        BindPhoneRequest request = new BindPhoneRequest();
        request.phone = phone;
        request.lx = lx;
        request.openid = openid;
        request.smscode = smsCode;
        mService.bindPhone(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<UserInfo>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<UserInfo> response) {
                        UserInfo userInfo = response.getContent();
                        UserInfoManager.getInstance().setUserInfo(userInfo);
                        mView.loginSuccess();
                    }
                });
    }

    @Override
    public void sendCode(String phone) {
        mView.showLoading();
        SmsCodeRequest request = new SmsCodeRequest();
        request.phone = phone;
        mService.smsCode(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<SmsCodeEntity>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<SmsCodeEntity> response) {
                        mView.getSmsCodeSuccess();
                    }
                });
    }
}
